<template>
	<view class="">
		<view class="">
			<u-swipe-action :show="item.show" :index="index" v-for="(item, index) in list" :key="item.id" @click="click"
				@open="open" :options="options">
				<view class="top u-flex u-row-between mode" @click="select(item)">
					<view class="u-flex">
						<u-icon name="map-fill" color="#2D9AFE" size="40"></u-icon>
						<view class="u-margin-left-20" style="width: 500rpx;">
							<view>
								<text style="font-size: 32rx;font-weight: bold;">{{item.realname}}</text>
								<text
									style="color:#999999;margin-left: 20rpx;margin-right: 20rpx;">{{item.phone}}</text>
								<u-tag text="默认" type="primary" size="mini" v-if="item.default_addr" />
							</view>
							<view class="">
								{{item.province}}{{item.city}}{{item.county}}{{item.address}}
							</view>
						</view>
					</view>

				</view>
			</u-swipe-action>
		</view>
		<view class="bottomFix" style="bottom: 50rpx;">
			<u-button @click="submit" type="primary"
				:custom-style="{width:'500rpx',marginTop:'40rpx',backgroundColor:'##2D9AFE'}" shape="circle">新增地址
			</u-button>
		</view>
		
		
		<view class="empty" v-if="!list.length" >
				<u-empty text="暂无收货地址" mode="address"></u-empty>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				show: false,
				options: [

					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}, {
						text: '编辑',
						style: {
							backgroundColor: '#50a0f0'
						}
					}
				]


			};
		},
		onShow() {
			this.getAddress()
		},
		methods: {
			select(item) {
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				if (prevPage.route == "pages/sure_book/index") {
					prevPage.$vm.address_id = item.id; //修改上一页面的 couponNumber 参数值为 value
					uni.navigateBack({
						delta: 1
					})
				}
			},
			click(index, index1) {
				if (!index1) {
					this.$u.post('/user/addressDel', {
						address_id: this.list[index].id
					}, {}).then(res => {
						this.list.splice(index, 1);
						this.$u.toast(`删除成功`);
					}).catch(err => {})
				} else {
					uni.navigateTo({
						url: "./addAddress?id=" + this.list[index].id
					})

				}




			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if (index != idx) this.list[idx].show = false;
				})
			},
			getAddress() {
				this.$u.post('/user/address', {

				}, {}).then(res => {

					res.forEach(item => {
						item.show = false
					})
					this.list = res;
				}).catch(err => {})

			},
			submit() {
				uni.navigateTo({
					url: "./addAddress"
				})
			},
			edit(item) {

			}
		},

	};
</script>



<style>
	.mode {
		padding: 30rpx !important;
	}

	.top {

		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
	}
</style>
